package net.towee.model.repo.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.towee.model.account.AccountIdentity;
import net.towee.model.discussion.DiscussionSubject;
import net.towee.model.group.GroupIdentity;
import net.towee.model.repo.AttachmentIdentity;
import net.towee.model.repo.DocumentIdentity;
import net.towee.model.repo.DocumentType;
import net.towee.model.repo.DocumentTypesContainer;
import net.towee.server.persistence.impl.MongoDbStore;

import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.PostLoad;
import com.google.code.morphia.annotations.Reference;

/**
 * Identity of this document
 * @author c58
 */
@Entity(MongoDbStore.DOCUMENTS_IDENTITY_COLLECTION)
public class DocumentIdentityImpl implements DocumentIdentity, DocumentTypesContainer {
	@Id private String id;
	private String name;
	private List<DocumentType> types;
	private boolean trash;
	
	// For better searching
	private Date creationDate;
	private Date updateDate;
	
	@Reference private AccountIdentity author;
	@Reference private GroupIdentity group;
	
	public DocumentIdentityImpl() {}
	public DocumentIdentityImpl(String id, String name, AccountIdentity aidnt, GroupIdentity gidnt, DocumentType... type) {
		this.id = id;
		this.name = name;
		this.author = aidnt;
		this.group = gidnt;
		this.trash = false;
		
		this.types = new ArrayList<DocumentType>();
		this.creationDate = new Date();
		this.updateDate = creationDate; 
		
		// Add types
		if(type != null) 
			for(DocumentType t : type)
				addDocumentType(t);
	}
	
	@PostLoad
	public void createEmptyFields() {
		if(this.types == null)
			this.types = new ArrayList<DocumentType>();
	}
	
	@Override
	public String getId() {
		return id;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public AccountIdentity getAuthor() {
		return author;
	}

	@Override
	public GroupIdentity getGroup() {
		return group;
	}

	@Override
	public DocumentTypesContainer getDocumentTypes() {
		return this;
	}

	@Override
	public void addDocumentType(DocumentType type) {
		if( !types.contains(type) )
			types.add(type);
	}

	@Override
	public void removeDocumentType(DocumentType type) {
		types.remove(type);
	}

	@Override
	public boolean hasDocumentType(DocumentType type) {
		return types.contains(type);
	}
	
	@Override
	public List<DocumentType> getTypesList() {
		return types;
	}
	@Override
	public void setId(String id) {
		this.id = id;
	}
	
	@Override
	public boolean equals(Object o) {
		if(o == this)
			return true;
		if(o instanceof DocumentIdentity && ((DocumentIdentity)o).getId().equals(getId()))
			return true;
		
		return false;
	}
	@Override
	public void setAuthor(AccountIdentity accountIdentity) {
		this.author = accountIdentity;
	}
	@Override
	public void setGroup(GroupIdentity groupIdentity) {
		this.group = groupIdentity;
	}
	@Override
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	@Override
	public void setUpdateDate(Date creationDate) {
		this.updateDate = creationDate;
	}
	@Override
	public Date getCreationDate() {
		return creationDate;
	}

	@Override
	public Date getUpdateDate() {
		return updateDate;
	}
}
